<?php

namespace seecms\controller;

use seecms\lib\builder\button\EditButton;
use seecms\lib\builder\control\InputControl;
use seecms\lib\builder\query\DatetimeQuery;
use seecms\lib\builder\query\SelectQuery;
use seecms\lib\Transfer;
use seecms\See;
use seecms\lib\Auth;
use seecms\lib\UserPermission;
use seecms\SeeConst;
use seecms\SeeLang;

class UserRuleApply extends Resource
{
    protected $table_name = 'see_user_rule_apply';

    protected $dictionary_name = 'user_rule_apply';

    protected function pageTitle(): string
    {
        return __('user_rule_apply.page_title');
    }

    protected function leftToolbars(): array
    {
        return [];
    }

    protected function query(): array
    {
        $users = See::table()->user->order('id desc')->column('nickname', 'id');

        return [
            SelectQuery::init('user_id')->setOptions($users)->setPlaceholder(SeeLang::user('select_user')),
            SelectQuery::init('status')->setOptions(Transfer::translateVerifyStatus())->setValue(SeeConst::STATUS_PADDING)->setPlaceholder(SeeLang::rule('select_verify_status')),
            DatetimeQuery::init('created_at')->setRange(),
        ];
    }

    protected function columnFilter($data)
    {
        if ($data['data'] ?? []) {
            foreach ($data['data'] as &$item) {
                $item['status_text'] = Transfer::translateStatus($item['status']);
            }
            unset($item);
        }

        return $data;
    }

    protected function columns(): array
    {
        return [
            'id.100',
            'user_id.100',
            'title',
            'url.300',
            'remark',
            'status_text',
            'reason',
            'audited_at.180',
            'audit_user',
            'created_at',
        ];
    }

    public function actions(): array
    {
        return [
            EditButton::init('verify')
        ];
    }

    public function controls(): array
    {
        return [
            InputControl::init('title')->setDisabled(),
            InputControl::init('url')->setDisabled(),
            InputControl::init('remark')->setDisabled(),
            ['status', Transfer::translateVerifyStatus()],
            'reason.false',
        ];
    }


    public function update(): array
    {
        $id = See::request()->param('id');
        $status = See::request()->param('status');

        $user_apply = See::table()->user_rule_apply->find($id);
        if (empty($user_apply)) {
            return $this->fail(SeeLang::rule('apply_not_found'));
        }

        if ($user_apply['status'] == SeeConst::STATUS_PASS) {
            return $this->fail(SeeLang::rule('verify_passed'));
        }

        if ($user_apply['status'] == $status) {
            return $this->fail(SeeLang::rule('verify_invalid'));
        }

        $url = $user_apply['url'];
        $rule = See::table()->user_rule->where('url', $url)->find();
        if (empty($rule)) {
            return $this->fail(SeeLang::rule('not_found'));
        }

        $user_id = $user_apply['user_id'];
        $user_permission = UserPermission::init($user_id);

        $user_rule = $user_permission->rule();
        $user_rule[] = $rule['id'];

        // 增加用户权限
        $user_rule = array_unique($user_rule);
        $user_rule = implode(',', $user_rule);

        See::table()->user_permission->updateOrCreate(compact('user_id'), ['rule' => $user_rule]);

        $data = [];
        $data['status'] = $status;
        $data['reason'] = See::request()->post('reason');
        $data['audited_at'] = date('Y-m-d H:i:s');
        $data['audit_user'] = Auth::user()->uuid();

        $result = $this->db()->where('id', $id)->update($data);

        return $this->success($result);
    }
}